package cn.edu.scau.cmi.ema.domain;


import java.io.Serializable;

import java.lang.StringBuilder;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.persistence.*;
import javax.xml.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.*;
import java.util.Set;
import com.alibaba.fastjson.annotation.JSONField;

@Entity
@NamedQueries({ @NamedQuery(name = "findAllFoods", query = "select myFood from Food myFood"),
		@NamedQuery(name = "findFoodByName", query = "select myFood from Food myFood where myFood.name = ?1"),
		@NamedQuery(name = "findFoodByNameContaining", query = "select myFood from Food myFood where myFood.name like concat('%',?1,'%')"),
		@NamedQuery(name = "findFoodByDescription", query = "select myFood from Food myFood where myFood.description = ?1"),
		@NamedQuery(name = "findFoodByDescriptionContaining", query = "select myFood from Food myFood where myFood.description like concat('%',?1,'%')"),
		@NamedQuery(name = "findAllFoodsByBrand", query = "select myFood from Food myFood where myFood.brand.id = ?1"),
		@NamedQuery(name = "findAllFoodsByExpire", query = "select myFood from Food myFood where myFood.expire.id = ?1"),
		@NamedQuery(name = "findAllFoodsByStoremethod", query = "select myFood from Food myFood where myFood.storemethod.id = ?1"),
		@NamedQuery(name = "findAllFoodsByProducer", query = "select myFood from Food myFood where myFood.producer.id = ?1"),
		@NamedQuery(name = "findAllFoodsByFoodtype", query = "select myFood from Food myFood where myFood.foodtype.id = ?1"),
		@NamedQuery(name = "findAllFoodsByProductcode", query = "select myFood from Food myFood where myFood.productcode.id = ?1"),
		@NamedQuery(name = "findAllFoodsByTaste", query = "select myFood from Food myFood where myFood.taste.id = ?1"),
		@NamedQuery(name = "findFoodById", query = "select myFood from Food myFood where myFood.id = ?1"),
		@NamedQuery(name = "findFoodByPrimaryKey", query = "select myFood from Food myFood where myFood.id = ?1 "),
		@NamedQuery(name = "deleteFoodsByIds", query = "delete Food where id in (?1)"),
		@NamedQuery(name = "findFoodsByIds", query = "select myFood from Food myFood where myFood.id in (?1)"),
		@NamedQuery(name = "findFoodByPhyscialPrimaryKey", query = "select myFood from Food myFood where  myFood.name = ?1 and myFood.foodtype.id = ?2 and myFood.productcode.id = ?3 and myFood.brand.id = ?4"),
		@NamedQuery(name = "countFoodsByName", query = "select count(myFood)  from Food myFood where myFood.name like concat('%',?1,'%')") })
@Table(catalog = "ema", name = "food")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(namespace = "ema/cn/edu/scau/cmi/ema/domain", name = "Food")
@XmlRootElement(namespace = "ema/cn/edu/scau/cmi/ema/domain")

public class Food implements Serializable, Comparable<Food> {

	private static final long serialVersionUID = 1L;

	@Column(name = "id", nullable = false)
	@Basic(fetch = FetchType.EAGER)
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@XmlElement
	Integer id;

	@Column(name = "name")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String name;

	@Column(name = "description")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String description;
	@JSONField(serialize = false)
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumns({ @JoinColumn(name = "brand", referencedColumnName = "id", nullable = false) })
	@XmlTransient
	Brand brand;
	@JSONField(serialize = false)
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumns({ @JoinColumn(name = "expire", referencedColumnName = "id", nullable = false) })
	@XmlTransient
	Expire expire;
	@JSONField(serialize = false)
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumns({ @JoinColumn(name = "storemethod", referencedColumnName = "id", nullable = false) })
	@XmlTransient
	Storemethod storemethod;
	@JSONField(serialize = false)
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumns({ @JoinColumn(name = "producer", referencedColumnName = "id", nullable = false) })
	@XmlTransient
	Producer producer;
	@JSONField(serialize = false)
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumns({ @JoinColumn(name = "foodtype", referencedColumnName = "id", nullable = false) })
	@XmlTransient
	Foodtype foodtype;
	@JSONField(serialize = false)
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumns({ @JoinColumn(name = "productcode", referencedColumnName = "id", nullable = false) })
	@XmlTransient
	Productcode productcode;
	@JSONField(serialize = false)
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumns({ @JoinColumn(name = "taste", referencedColumnName = "id", nullable = false) })
	@XmlTransient
	Taste taste;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "food", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Detectstandard> detectstandardsForFood;
	@JSONField(serialize = false)
	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(catalog = "ema", name = "food2packingmethod", joinColumns = {
			@JoinColumn(name = "food", referencedColumnName = "id") }, inverseJoinColumns = {
					@JoinColumn(name = "packingmethod", referencedColumnName = "id") })
	@XmlElement(name = "", namespace = "")
	Set<Packingmethod> packingmethodsForFood;
	@JSONField(serialize = false)
	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(catalog = "ema", name = "food2processmethod", joinColumns = {
			@JoinColumn(name = "food", referencedColumnName = "id") }, inverseJoinColumns = {
					@JoinColumn(name = "processmethod", referencedColumnName = "id") })
	@XmlElement(name = "", namespace = "")
	Set<Processmethod> processmethodsForFood;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "food", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Foodadditive> foodadditivesForFood;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "food", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Foodbatch> foodbatchsForFood;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "food", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Foodmaterial> foodmaterialsForFood;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "food", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Foodsaleaddress> foodsaleaddresssForFood;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "food", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Picture> picturesForFood;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "food", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Video> videosForFood;

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getId() {
		return this.id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return this.name;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getDescription() {
		return this.description;
	}

	public void setBrand(Brand brand) {
		this.brand = brand;
	}

	public Brand getBrand() {
		return brand;
	}

	public void setExpire(Expire expire) {
		this.expire = expire;
	}

	public Expire getExpire() {
		return expire;
	}

	public void setStoremethod(Storemethod storemethod) {
		this.storemethod = storemethod;
	}

	public Storemethod getStoremethod() {
		return storemethod;
	}

	public void setProducer(Producer producer) {
		this.producer = producer;
	}

	public Producer getProducer() {
		return producer;
	}

	public void setFoodtype(Foodtype foodtype) {
		this.foodtype = foodtype;
	}

	public Foodtype getFoodtype() {
		return foodtype;
	}

	public void setProductcode(Productcode productcode) {
		this.productcode = productcode;
	}

	public Productcode getProductcode() {
		return productcode;
	}

	public void setTaste(Taste taste) {
		this.taste = taste;
	}

	public Taste getTaste() {
		return taste;
	}

	public void setDetectstandardsForFood(Set<Detectstandard> detectstandardsForFood) {
		this.detectstandardsForFood = detectstandardsForFood;
	}

	public Set<Detectstandard> getDetectstandardsForFood() {
		if (detectstandardsForFood == null) {
			detectstandardsForFood = new java.util.LinkedHashSet<Detectstandard>();
		}
		return detectstandardsForFood;
	}

	public void setPackingmethodsForFood(Set<Packingmethod> packingmethodsForFood) {
		this.packingmethodsForFood = packingmethodsForFood;
	}

	public Set<Packingmethod> getPackingmethodsForFood() {
		if (packingmethodsForFood == null) {
			packingmethodsForFood = new java.util.LinkedHashSet<Packingmethod>();
		}
		return packingmethodsForFood;
	}

	public void setProcessmethodsForFood(Set<Processmethod> processmethodsForFood) {
		this.processmethodsForFood = processmethodsForFood;
	}

	public Set<Processmethod> getProcessmethodsForFood() {
		if (processmethodsForFood == null) {
			processmethodsForFood = new java.util.LinkedHashSet<Processmethod>();
		}
		return processmethodsForFood;
	}

	public void setFoodadditivesForFood(Set<Foodadditive> foodadditivesForFood) {
		this.foodadditivesForFood = foodadditivesForFood;
	}

	public Set<Foodadditive> getFoodadditivesForFood() {
		if (foodadditivesForFood == null) {
			foodadditivesForFood = new java.util.LinkedHashSet<Foodadditive>();
		}
		return foodadditivesForFood;
	}

	public void setFoodbatchsForFood(Set<Foodbatch> foodbatchsForFood) {
		this.foodbatchsForFood = foodbatchsForFood;
	}

	public Set<Foodbatch> getFoodbatchsForFood() {
		if (foodbatchsForFood == null) {
			foodbatchsForFood = new java.util.LinkedHashSet<Foodbatch>();
		}
		return foodbatchsForFood;
	}

	public void setFoodmaterialsForFood(Set<Foodmaterial> foodmaterialsForFood) {
		this.foodmaterialsForFood = foodmaterialsForFood;
	}

	public Set<Foodmaterial> getFoodmaterialsForFood() {
		if (foodmaterialsForFood == null) {
			foodmaterialsForFood = new java.util.LinkedHashSet<Foodmaterial>();
		}
		return foodmaterialsForFood;
	}

	public void setFoodsaleaddresssForFood(Set<Foodsaleaddress> foodsaleaddresssForFood) {
		this.foodsaleaddresssForFood = foodsaleaddresssForFood;
	}

	public Set<Foodsaleaddress> getFoodsaleaddresssForFood() {
		if (foodsaleaddresssForFood == null) {
			foodsaleaddresssForFood = new java.util.LinkedHashSet<Foodsaleaddress>();
		}
		return foodsaleaddresssForFood;
	}

	public void setPicturesForFood(Set<Picture> picturesForFood) {
		this.picturesForFood = picturesForFood;
	}

	public Set<Picture> getPicturesForFood() {
		if (picturesForFood == null) {
			picturesForFood = new java.util.LinkedHashSet<Picture>();
		}
		return picturesForFood;
	}

	public void setVideosForFood(Set<Video> videosForFood) {
		this.videosForFood = videosForFood;
	}

	public Set<Video> getVideosForFood() {
		if (videosForFood == null) {
			videosForFood = new java.util.LinkedHashSet<Video>();
		}
		return videosForFood;
	}

	public void copy(Food that) {

		setId(that.getId());

		setName(that.getName());
		setDescription(that.getDescription());

		setBrand(that.getBrand());

		setExpire(that.getExpire());

		setStoremethod(that.getStoremethod());

		setProducer(that.getProducer());

		setFoodtype(that.getFoodtype());

		setProductcode(that.getProductcode());

		setTaste(that.getTaste());

		setDetectstandardsForFood(new java.util.LinkedHashSet<Detectstandard>(
				that.getDetectstandardsForFood()));

		setPackingmethodsForFood(
				new java.util.LinkedHashSet<Packingmethod>(that.getPackingmethodsForFood()));

		setProcessmethodsForFood(
				new java.util.LinkedHashSet<Processmethod>(that.getProcessmethodsForFood()));

		setFoodadditivesForFood(
				new java.util.LinkedHashSet<Foodadditive>(that.getFoodadditivesForFood()));

		setFoodbatchsForFood(
				new java.util.LinkedHashSet<Foodbatch>(that.getFoodbatchsForFood()));

		setFoodmaterialsForFood(
				new java.util.LinkedHashSet<Foodmaterial>(that.getFoodmaterialsForFood()));

		setFoodsaleaddresssForFood(new java.util.LinkedHashSet<Foodsaleaddress>(
				that.getFoodsaleaddresssForFood()));

		setPicturesForFood(new java.util.LinkedHashSet<Picture>(that.getPicturesForFood()));

		setVideosForFood(new java.util.LinkedHashSet<Video>(that.getVideosForFood()));

	}

	public Food() {

	}

	@Override
	public String toString() {

		StringBuilder buffer = new StringBuilder();

		buffer.append("id=[").append(id).append("] ");

		buffer.append("name=[").append(name).append("] ");
		buffer.append("description=[").append(description).append("] ");

		return buffer.toString();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = (int) (prime * result + ((id == null) ? 0 : id.hashCode()));
		return result;
	}

	public boolean equals(Object obj) {
		if (obj == this)
			return true;
		if (!(obj instanceof Food))
			return false;
		Food comparedFood = (Food) obj;
		if (!CheckPropertyEqual(this.name, comparedFood.name)) {
			return false;
		}
		if (!CheckPropertyEqual(this.foodtype.id, comparedFood.foodtype.id)) {
			return false;
		}
		if (!CheckPropertyEqual(this.productcode.id, comparedFood.productcode.id)) {
			return false;
		}
		if (!CheckPropertyEqual(this.brand.id, comparedFood.brand.id)) {
			return false;
		}
		return true;
	}

	public static <T> boolean CheckPropertyEqual(T originProperty, T comparedProperty) {
		// 基本类型经过参数传递后会自动装箱
		String[] className = originProperty.getClass().getName().split("\\.");
		if (className[0].equals("java") && className[1].equals("math")) {
			return ((Comparable) originProperty).compareTo(comparedProperty) == 0 ? true : false;
		} else {
			return originProperty.equals(comparedProperty);
		}
	}

	@Override
	public int compareTo(Food o) {
		if (this.getId() > o.getId()) {
			return 1;
		} else if (this.getId() == o.getId()) {
			return 0;
		} else {
			return -1;
		}

	}
}
